package mongodb

import (
	"context"
	"github.com/spf13/viper"
	"go.mongodb.org/mongo-driver/mongo"
	"go.mongodb.org/mongo-driver/mongo/options"
	"golang-sgg-daijia/pkg/log"
)

type MongoClient struct {
	mongoDB *mongo.Client
	//rdb    *redis.Client
	logger *log.Logger
	conf   *viper.Viper
}

func NewMongoClient(
	conf *viper.Viper,
	logger *log.Logger,
) *MongoClient {
	clientOptions := options.Client().ApplyURI(conf.GetString("data.mongo.uri")).SetAuth(
		options.Credential{
			Username: conf.GetString("data.mongo.username"),
			Password: conf.GetString("data.mongo.password"),
		})
	client, err := mongo.Connect(context.TODO(), clientOptions)
	if err != nil {
		panic(err)
	}
	return &MongoClient{
		mongoDB: client,
		logger:  logger,
		conf:    conf,
	}
}

func (r *MongoClient) DB() *mongo.Database {
	return r.mongoDB.Database(r.conf.GetString("data.mongo.database"))
}
